<template>
  <div class="publish-container">
    <el-card>
      <div slot="header" class="clearfix">
        <Breadcrumb>
          编辑文章
        </Breadcrumb>
      </div>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <!-- <el-form-item label="内容">
          <el-input type="textarea" v-model="form.content"></el-input>
        </el-form-item> -->
        <el-form-item label="内容" prop="content">
         <quill-editor
          v-model="form.content"
          :options="editorOption"></quill-editor>
        </el-form-item>
        <el-form-item label="封面">
          <el-radio-group v-model="form.coverType" @change="hReSetImageList">
            <el-radio :label="1" name="type">单图</el-radio>
            <el-radio :label="3" name="type">三图</el-radio>
            <el-radio :label="0" name="type">无图</el-radio>
            <el-radio :label="-1" name="type">自动</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="" v-if="form.coverType>0">
          <el-row>
            <el-col :span="6" v-for="(item,idx) in form.coverType" :key="idx">
                {{idx}}
                <Cover v-model="form.coverImages[idx]"/>
            </el-col>
          </el-row>
        </el-form-item>

        <Channel v-model="form.channelId" />
        <el-form-item>
          <el-button type="primary" @click="hSave">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'EditArticle',
  components: {
    quillEditor
  },
  data () {
    return {
      editorOption: {
        // 对富文本编辑器的配置
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }, { header: 3 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      form: {
        id: '',
        title: '这里有一篇好文章',
        content: '这里有一篇好文章，这里有一篇好文章',
        coverType: 0, // 封面类型：-1（自动）， 0（无图）， 1（1张图）， 3（3张图）
        coverImages: [], // 保存对应的封面图的地址
        channelId: ''
      },
      // 表单验证规则
      // 格式： 键：要验证的字段, 值是一个数组，每一项就是一条规则
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.loadDetail()
  },
  methods: {
    async loadDetail () {
      const result = await this.$http({
        method: 'GET',
        url: '/mp/v1_0/articles/' + this.$route.params.id
      })
      console.log(result)
      this.form.title = result.data.data.title
      this.form.content = result.data.data.content
      this.form.coverType = result.data.data.cover.type
      this.form.coverImages = result.data.data.cover.images
      this.form.channelId = result.data.data.channel_id
      this.form.id = result.data.data.id
    },
    // 选择不同封面类型，需要重置一下图片数组
    hReSetImageList () {
      // 0,1,-1   =====> 3
      if (this.form.coverType === 3) {
      } else if (this.form.coverType === 1) {
        //  3 === > 1
        // this.form.coverImages.splice(1)
        this.form.coverImages = [this.form.coverImages[0]]
      }
    },
    async hSave () {
      // 用户点击了保存草稿
      // 1. 收集文章信息
      console.log(this.form)
      // 2. 验证数据- el-form表单

      this.$refs.form.validate(result => {
        console.log(result)
        // 验证通过
        if (result) {
          this.doSave()
        }
      })
    },
    async doSave () {
      // TODO 保存loading效果
      // 3. 调用接口
      try {
        const result = await this.$http({
          method: 'PUT',
          url: '/mp/v1_0/articles/' + this.form.id,
          data: {
            title: this.form.title,
            content: this.form.content,
            cover: {
              type: this.form.coverType,
              images: this.form.coverImages
            },
            channel_id: this.form.channelId
          }
        })
        console.log(result)
        // 1.提示用户ok
        this.$message.success('修改成功')
        // 2. 清空表单
        this.form.title = ''
      } catch (err) {
        console.log(err)
        this.$message.error('操作失败')
      }
    },
    onSubmit () {
      console.log(this.form)
    }
  }
}
</script>

<style scoped lang='less'>
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
